package com.icow.wms.basic.service.impl;

import java.util.List;

import com.icow.common.exception.CustomException;
import com.icow.common.utils.DateUtils;
import com.icow.common.utils.StringUtils;
import com.icow.framework.web.exception.GlobalExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.icow.wms.basic.mapper.WmsMatListMapper;
import com.icow.wms.basic.domain.WmsMatList;
import com.icow.wms.basic.service.IWmsMatListService;

/**
 * 物料需求Service业务层处理
 *
 * @author lqq
 * @date 2022-01-14
 */
@Service
@SuppressWarnings("all")
public class WmsMatListServiceImpl implements IWmsMatListService {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @Autowired
    private WmsMatListMapper wmsMatListMapper;

    /**
     * 查询物料需求
     *
     * @param id 物料需求ID
     * @return 物料需求
     */
    @Override
    public WmsMatList selectWmsMatListById(Long id) {
        return wmsMatListMapper.selectWmsMatListById(id);
    }

    /**
     * 查询物料需求列表
     *
     * @param wmsMatList 物料需求
     * @return 物料需求
     */
    @Override
    public List<WmsMatList> selectWmsMatListList(WmsMatList wmsMatList) {
        return wmsMatListMapper.selectWmsMatListList(wmsMatList);
    }

    /**
     * 新增物料需求
     *
     * @param wmsMatList 物料需求
     * @return 结果
     */
    @Override
    public int insertWmsMatList(WmsMatList wmsMatList) {
        wmsMatList.setCreateTime(DateUtils.getNowDate());
        return wmsMatListMapper.insertWmsMatList(wmsMatList);
    }

    /**
     * 修改物料需求
     *
     * @param wmsMatList 物料需求
     * @return 结果
     */
    @Override
    public int updateWmsMatList(WmsMatList wmsMatList) {
        wmsMatList.setUpdateTime(DateUtils.getNowDate());
        return wmsMatListMapper.updateWmsMatList(wmsMatList);
    }

    /**
     * 批量删除物料需求
     *
     * @param ids 需要删除的物料需求ID
     * @return 结果
     */
    @Override
    public int deleteWmsMatListByIds(Long[] ids) {
        return wmsMatListMapper.deleteWmsMatListByIds(ids);
    }

    /**
     * 删除物料需求信息
     *
     * @param id 物料需求ID
     * @return 结果
     */
    @Override
    public int deleteWmsMatListById(Long id) {
        return wmsMatListMapper.deleteWmsMatListById(id);
    }

    @Override
    public int deleteWmsMatList() {
        return wmsMatListMapper.deleteWmsMatList();
    }

    /**
     * 导入数据
     *
     * @param wmsMatListList  数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importWmsMatList(List<WmsMatList> wmsMatListList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(wmsMatListList) || wmsMatListList.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (WmsMatList item : wmsMatListList) {
            try {
                // 验证是否存在这个数据
                // 依据客户要求 这里无需验证是否存在
                //WmsMatList u = wmsMatListMapper.selectWmsMatListBySn(item.getSn());
                //if (StringUtils.isNull(u)) {
                    item.setCreateBy(operName);
                    item.setStatus("N");
                    item.setCreateTime(DateUtils.getNowDate());
                    this.insertWmsMatList(item);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编码 " + item.getSn() + " 导入成功");
//                } else if (isUpdateSupport) {
//                    item.setUpdateTime(DateUtils.getNowDate());
//                    item.setUpdateBy(operName);
//                    this.updateWmsMatList(item);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、编码 " + item.getSn() + " 更新成功");
//                } else {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、编码 " + item.getSn() + " 已存在");
//                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编码 " + item.getSn() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
